using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.EditControls;
using Janus.Windows.Schedule;
using Janus.Windows.GridEX;
namespace Janus.NorthwindDemo2007
{
	public partial class ReminderForm
	{

		private Janus.Windows.Schedule.Schedule mSchedule;
		private ArrayList reminderAppointments = new ArrayList();

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			NorthwindApp.MainForm.VisualStyleManager1.AddControl(this, true);
            this.Owner = NorthwindApp.MainForm;
		}
		private void PopulateSnoozeTimeCombo()
		{

			cboSnoozeTime.Items.Add(new UIComboBoxItem("1 minute", new TimeSpan(0, 1, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("5 minutes", new TimeSpan(0, 5, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("10 minutes", new TimeSpan(0, 10, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("15 minutes", new TimeSpan(0, 15, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("30 minutes", new TimeSpan(0, 30, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("1 hour", new TimeSpan(1, 0, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("2 hours", new TimeSpan(2, 0, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("4 hours", new TimeSpan(4, 0, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("8 hours", new TimeSpan(8, 0, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("1 day", new TimeSpan(1, 0, 0, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("2 days", new TimeSpan(2, 0, 0, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("4 days", new TimeSpan(4, 0, 0, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("1 week", new TimeSpan(7, 0, 0, 0)));
			cboSnoozeTime.Items.Add(new UIComboBoxItem("2 weeks", new TimeSpan(14, 0, 0, 0)));
			cboSnoozeTime.SelectedItem = cboSnoozeTime.Items[0];
		}

		public void ShowReminderForm(Janus.Windows.Schedule.Schedule schedule, Janus.Windows.Schedule.ScheduleAppointment appointment)
		{
			this.mSchedule = schedule;
            mSchedule.AppointmentRemoved += new AppointmentEventHandler(schedule_AppointmentRemoved);
            mSchedule.AppointmentChanged += new AppointmentChangeEventHandler(schedule_AppointmentChanged);

			this.AddItem(appointment);
			this.GridEX1.SetDataBinding(this.reminderAppointments, "");
			this.Show();
		}

		private string GetDueTimeText(TimeSpan dueTime)
		{
			string text = null;
			if (Math.Abs(dueTime.Days) > 0)
			{
				text = Math.Abs(dueTime.Days).ToString() + " days";
			}
			else if (Math.Abs(dueTime.Hours) > 0)
			{
				text = Math.Abs(dueTime.Hours).ToString() + " hours";
			}
			else
			{
				text = Math.Abs(dueTime.Minutes).ToString() + " minutes";
			}

			if (TimeSpan.Compare(dueTime, TimeSpan.Zero) < 0)
			{
				text += " overdue";
			}

			return text;
		}

		public void AddItem(Janus.Windows.Schedule.ScheduleAppointment app)
		{
			if (! this.reminderAppointments.Contains(app))
			{
				this.reminderAppointments.Add(app);

				this.GridEX1.Refetch();
				if (this.GridEX1.RowCount > 0)
				{
					this.GridEX1.Row = this.GridEX1.RowCount - 1;
				}
			}
		}


		private void btnDismiss_Click(object sender, System.EventArgs e)
		{
			GridEXRow row = this.GridEX1.GetRow();
			if (row != null)
			{
                
				ScheduleAppointment dismissApp = (ScheduleAppointment)row.DataRow;
				dismissApp.Reminder.Enabled = false;
				this.GridEX1.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.True;
				this.GridEX1.Delete();
				this.GridEX1.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.False;
				if (this.GridEX1.RowCount == 0)
				{
					this.Close();
					return;
				}
			}
			this.GridEX1.Row = 0;
		}

		private void btnSnooze_Click(object sender, System.EventArgs e)
		{
			GridEXRow row = this.GridEX1.GetRow();
			if (row != null)
			{

				ScheduleAppointment snoozeApp = (ScheduleAppointment)row.DataRow;
				snoozeApp.Reminder.SnoozedInterval = (TimeSpan)this.cboSnoozeTime.SelectedValue;

				snoozeApp.Reminder.Snooze();
				this.GridEX1.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.True;
				this.GridEX1.Delete();
				this.GridEX1.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.False;
				if (this.GridEX1.RowCount == 0)
				{
					this.Close();
					return;
				}
			}
			this.GridEX1.Row = 0;

		}

		private void btnOpen_Click(object sender, System.EventArgs e)
		{
			GridEXRow row = this.GridEX1.GetRow();
			if (row != null)
			{
				AppointmentsForm form = new AppointmentsForm();
				ScheduleAppointment app = (ScheduleAppointment)row.DataRow;
				form.CreateAppointmentForm(this.mSchedule, app.StartTime, app.Duration, app);
			}
			else
			{
				this.GridEX1.Row = 0;
			}
		}

		private void btnDismissAll_Click(object sender, System.EventArgs e)
		{
			this.GridEX1.DataSource = null;
			foreach (ScheduleAppointment app in this.reminderAppointments)
			{
				app.Reminder.Enabled = false;
			}
			this.Close();
		}

		private void schedule_AppointmentRemoved(object sender, AppointmentEventArgs e)
		{
			if (this.reminderAppointments.Contains(e.Appointment))
			{
				this.RemoveAppointment(e.Appointment);
			}
		}

		private void schedule_AppointmentChanged(object sender, AppointmentChangeEventArgs e)
		{
			if (this.reminderAppointments.Contains(e.Appointment))
			{
				ListViewItem item = null;
				switch (e.PropertyChanged)
				{
					case AppointmentChange.ReminderEnabled:
						if (! e.Appointment.Reminder.Enabled)
						{
							this.RemoveAppointment(e.Appointment);
						}
						break;
					case AppointmentChange.StartTime:
					break;
					case AppointmentChange.ReminderInterval:
						if (e.Appointment.Reminder.GetReminderDateTime() > DateTime.Now)
						{
							this.RemoveAppointment(e.Appointment);
						}
						else
						{
							GridEXRow row = this.GridEX1.GetRow(e.Appointment);
							if (row != null)
							{
								TimeSpan dueTime = e.Appointment.StartTime.Subtract(DateTime.Now);
								row.Cells["clmDue"].Text = this.GetDueTimeText(dueTime);
								if (row.Selected)
								{
									this.lblStartTime.Text = e.Appointment.StartTime.ToString("f");
								}
							}
						}
						break;
				}
			}
		}

		private void GridEX1_LoadingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
		{

			ScheduleAppointment appointment = (ScheduleAppointment)e.Row.DataRow;

			TimeSpan dueTime = appointment.StartTime.Subtract(DateTime.Now);
			e.Row.Cells["DueIn"].Value = dueTime.TotalMinutes;
			e.Row.Cells["DueIn"].Text = this.GetDueTimeText(dueTime);
		}

		private void GridEX1_SelectionChanged(object sender, System.EventArgs e)
		{
			GridEXRow row = this.GridEX1.GetRow();
			if (row != null)
			{
				Janus.Windows.Schedule.ScheduleAppointment app = (ScheduleAppointment)row.DataRow;
				this.lblStartTime.Text = app.StartTime.ToString("f");
				this.lblText.Text = "     " + app.Text;
			}
		}

		private void RemoveAppointment(ScheduleAppointment appointment)
		{
			GridEXRow row = GridEX1.GetRow(appointment);
			if (row != null)
			{
				GridEX1.MoveTo(row);
				GridEX1.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.True;
				GridEX1.Delete();
				GridEX1.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.False;
				if (GridEX1.RowCount == 0)
				{
					this.Close();
				}
			}
		}

		private void GridEX1_DoubleClick(object sender, System.EventArgs e)
		{
			if (GridEX1.HitTest() == GridArea.Cell)
			{
				this.btnOpen.PerformClick();
			}
		}

		protected override void OnClosed(System.EventArgs e)
		{
			base.OnClosed(e);
			this.mSchedule = null;
		}

	}
} //end of root namespace